package com.avic.rong.activity.interest;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.adapter.FocusAreaAdapter;
import com.avic.rong.api.mine.InterestedApi;
import com.avic.rong.api.mine.UpdateInterestApi;
import com.avic.rong.bean.mine.InterestedBean;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

//我的—关注的领域
public class FocusAreaActivity extends BaseActivity {

    @BindView(R.id.btn_complete)
    Button btn_complete;


    @BindView(R.id.recv_focus)
    RecyclerView recv_focus;

    private ArrayList<InterestedBean.InterestListBean> mList = new ArrayList<>();
    private ArrayList<String> strings;//关注的领域
    private FocusAreaAdapter focusAreaAdapter;
    private String login_token;

    private StringBuffer str = new StringBuffer();
    private SharedPreferencesUtil sharedPreferencesUtil;
    ArrayList<String> foucs = new ArrayList<>();
    private ArrayList<String> labels = new ArrayList<>();

    @Override
    protected int getContentViewId() {
        return R.layout.activity_focus_area;
    }

    @Override
    protected void initView() {
        tvTitle.setText("关注的领域");


        sharedPreferencesUtil = SharedPreferencesUtil.getInstance(this);
        login_token = sharedPreferencesUtil.getLogintoken();


        focusAreaAdapter = new FocusAreaAdapter(this, mList);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int i) {
                int viewType = focusAreaAdapter.getItemViewType(i);
                if (viewType == 1) {
                    return 2;
                }
                return 1;
            }
        });
        recv_focus.setLayoutManager(gridLayoutManager);
        recv_focus.setAdapter(focusAreaAdapter);

        initData();

    }

    @Override
    protected void addListener() {

    }

    private void initData() {
        InterestedApi interestedApi = new InterestedApi(this,interestedListener );
        interestedApi.setToken(login_token);
        HttpManager.getInstance().doHttpDeal(interestedApi);
    }

    HttpOnNextListener<BaseResultEntity> interestedListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_200)){
                InterestedBean data = (InterestedBean) baseResultEntity.getData();
                String userLabel = data.getUserLabel();
                List<InterestedBean.InterestListBean> interestList = data.getInterestList();
                mList.addAll(interestList);
                focusAreaAdapter.setUserLable(userLabel);
                focusAreaAdapter.notifyDataSetChanged();
            }
        }
    };

    public static Intent queryIntent(Context context) {
        Intent intent = new Intent(context, FocusAreaActivity.class);
        return intent;
    }

    @OnClick({ R.id.btn_complete})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.btn_complete:
                updateInter();
                break;
        }
    }

    private void updateInter() {
        labels = focusAreaAdapter.getLabels();
        strings = focusAreaAdapter.getStrings();

        for (int i = 0; i < labels.size(); i++) {
            if (i==labels.size()-1){
                str.append(labels.get(i));
            }else {
                str.append(labels.get(i)+",");
            }
        }
        UpdateInterestApi updateInterApi = new UpdateInterestApi(this,updateInterListener );
        updateInterApi.setArgument(login_token,str.toString());
        HttpManager.getInstance().doHttpDeal(updateInterApi);
    }

    HttpOnNextListener<BaseResultEntity> updateInterListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            String data = (String) baseResultEntity.getData();
            if (data.equals("CODE_SUCCESS")){
                Toast toast = Toast.makeText(FocusAreaActivity.this, "修改成功", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER,0,0);
                toast.show();

                for (String str: strings) {
                    if (!foucs.contains(str)){
                        foucs.add(str);
                    }else {
                        return;
                    }
                }

                sharedPreferencesUtil.put("focus",foucs);
                Intent intent = new Intent("android.intent.action.CART_BROADCAST");
                intent.putExtra("interest","refresh");
                LocalBroadcastManager.getInstance(FocusAreaActivity.this).sendBroadcast(intent);
                sendBroadcast(intent);
                /*EventBus.getDefault().postSticky(new HomePageEvent());
                startActivity(Main3Activity.queryIntent(FocusAreaActivity.this));*/
                finish();
            }
        }
    };

}
